{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from io import StringIO \n",
    "from geopandas import GeoDataFrame\n",
    "from iclientpy.rest.api.model import DataItemType,PermissionType,DataPermissionType\n",
    "from iclientpy.portal import Portal,BaseLayerType,MapShareSettingBuilder,DataShareSettingBuilder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "portal=Portal('http://localhost:8090/iportal',token='o1glBvFVc76JWBoISTCIiHzNXyF4Tdyr9WwHRy8ytDtTJHEL19HLKrx6TGwLswynHJ0cNBzEv87MJK5SZ2NWZw..')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从CSV文件读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv(\"province.csv\")\n",
    "df[\"2015年\"]=pd.to_numeric(df[\"2015年\"],errors='coerce')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取地理位置数据，geonjson格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf=GeoDataFrame.from_file('./data.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将CSV数据与地理位置数据进行拼接，根据省份名称所在字段名进行数据拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=gdf.merge(df,left_on='name',right_on='地区')\n",
    "result.drop(['name'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上传数据到iportal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data_id = portal.upload_dataframe_as_json('all.json',result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "配置数据的共享信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_share=DataShareSettingBuilder().share_to_everyone(DataPermissionType.DOWNLOAD).build()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "portal.config_data_sharesetting(data_id,data_share)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将数据转换为Layer，便于生成地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "layer=portal.prepare_geojson_layer(data_id,'layer')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在iportal上根据Layer创建地图，默认底图为天地图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_id=portal.create_map(layers=[layer],epsgCode=3857,map_title='overlay')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搜索名称为share的群组，配置地图的共享权限信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "groups=portal.search_group(keywords=['share'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_share=MapShareSettingBuilder().share_to_group(groups[0].id,PermissionType.READWRITE).build()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "portal.config_map_sharesetting(map_id,map_share)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
